package org.jeecg.modules.activiti.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.DataFormat;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activiti.entity.FormDesign;
import org.jeecg.modules.activiti.entity.FormDesignData;
import org.jeecg.modules.activiti.mapper.FormDesignDataMapper;
import org.jeecg.modules.activiti.service.IFormDesignDataService;
import org.jeecg.modules.activiti.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Description: 流程业务扩展表
 * @author: 黄劲博
 * @date: 2021/6/21 9:50
*/
@Service
public class FormDesignDataServiceImpl extends ServiceImpl<FormDesignDataMapper, FormDesignData> implements IFormDesignDataService {

    @Autowired
    UserUtil userUtil;
    @Autowired
    FormDesignServiceImpl formDesignService;

    @Override
    public Map<String, Object> getHongData(String id) {
        Map<String, Object> res = new HashMap<>();
        Map<String, List> jsonDate = new HashMap<>();
        QueryWrapper<FormDesign> formDesignQueryWrapper = new QueryWrapper<>();
        formDesignQueryWrapper.eq("from_number", id);
        formDesignQueryWrapper.eq("status", "1");
        FormDesign formDesign = formDesignService.getOne(formDesignQueryWrapper);
        String data = formDesign.getData();
        jsonDate = JSON.parseObject(data, Map.class);
        //自定义表单组件信息
        List<Map<String, Object>> itemList = jsonDate.get("list");
        for (Map<String, Object> item : itemList) {
            if (!StringUtils.isEmpty(item.get("hongKey") + "")) {
                switch (item.get("hongKey")+ "") {
                    case "currUser":
                        getCurrUser(item, res);
                        break;
                    case "currDate":
                        getCurrDate(item, res);
                        break;
                    default:
                }
            }
        }

        return res;
    }

    private Map<String, Object> getCurrUser(Map<String, Object> item, Map<String, Object> res) {
        LoginUser loginUser = userUtil.getCurrUser();
        Map<String, Object> options = (Map<String, Object>) item.get("options");
        switch (options.get("hongType")+ "") {
            case "currUserId":
                res.put(item.get("key") + "", loginUser.getId());
                break;
            case "currUserName":
                res.put(item.get("key") + "", loginUser.getRealname());
                break;
            case "currUserDeptId":
                res.put(item.get("key") + "", loginUser.getOrgCode());
                break;
            case "currUserDeptName":
                res.put(item.get("key") + "", loginUser.getDepartIds());
                break;
            default:
        }
        return res;
    }

    private Map<String, Object> getCurrDate(Map<String, Object> item, Map<String, Object> res) {
        Map<String, Object> options =(Map<String, Object>) item.get("options");
        String formatString = options.get("format") + "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
        res.put(item.get("key") + "", simpleDateFormat.format(new Date()));
        return res;
    }

    //private转Map
    public static Map<String, Object> getObjectToMap(Object obj){
        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> cla = obj.getClass();
        Field[] fields = cla.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String keyName = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (value == null)
                value = "";
            map.put(keyName, value);
        }
        return map;
    }


}
